package controller;

import entity.BrainTrain;
import entity.Phase;

/**
 * @author Ayase
 * @date 2020/8/13-14:36
 */
public class TrainController {
    private BrainTrain brainTrain;

    private int nowPhase;

    private int width;

    public TrainController(int width){
        this.width = width;
        readPhase();
    }

    /**
     * @MethodName readPhase
     * @Description TODO 读取文段
     * @Param []
     * @Return void
     * @author Ayase
     * @date 14:16
     */
    public void readPhase(){
        brainTrain = BrainTrain.getInstance();
        brainTrain.greneratePhase();
        nowPhase = 0;
    }

    /**
     * @MethodName getPhaseHtml
     * @Description TODO 返回文段的html
     * @Param [i]
     * @Return java.lang.String
     * @author Ayase
     * @date 14:13
     */
    public String getPhaseHtml(){
        if(brainTrain == null){
            throw new IllegalStateException("题库尚未加载！");
        }
        if(!hasNext()){
            return null;
        }

        Phase p = brainTrain.getPhases().get(nowPhase++);
        String preStr = "<div style=\"font-size:30px;width:"+width+"\">\n<span>"+p.getPreStr()+"</span>\n";
        String word = "<span style=\"color:red\">" + p.getWord() +"</span>\n";
        String postStr = "<span>" + p.getPostStr() +"</span>\n</div>";

        return preStr+word+postStr;
    }

    /**
     * @MethodName hasNext
     * @Description TODO 检查是否仍有题目
     * @Param []
     * @Return boolean
     * @author Ayase
     * @date 14:53
     */
    public boolean hasNext(){
        return nowPhase<brainTrain.getPhases().size();
    }

    /**
     * @MethodName getDifficult
     * @Description TODO 返回当前难度
     * @Param []
     * @Return int
     * @author Ayase
     * @date 14:54
     */
    public int getDifficult(){
        return brainTrain.getDifficult();
    }

    public double calcuCR(String[] answers){
        brainTrain.check(answers);
        return brainTrain.calcuCR();
    }

    public void nextTrain(double cr){
        if(cr <= 0.65){
            levelDown();
        }
        if(cr >= 0.85){
            levelUp();
        }
        readPhase();
    }

    /**
     * @MethodName levelUp
     * @Description TODO 难度上升
     * @Param []
     * @Return void
     * @author Ayase
     * @date 16:41
     */
    public void levelUp(){
        brainTrain.setDifficult(brainTrain.getDifficult()+1);
    }

    /**
     * @MethodName levelDown
     * @Description TODO
     * @Param []
     * @Return void
     * @author Ayase
     * @date 16:42
     */
    public void levelDown(){
        if(brainTrain.getDifficult() == 1){
            return;
        }
        brainTrain.setDifficult(brainTrain.getDifficult()-1);
    }

    public String getAnswers(){
        String[] a = new String[getDifficult()];
        for(int i = 0 ; i < getDifficult() ; i++){
            a[i] = brainTrain.getPhases().get(i).getWord();
        }

        StringBuffer answer = new StringBuffer();
        for(int i = 0 ; i < getDifficult() ;i++){
            answer.append((i+1)+":"+a[i]+"  ");
        }
        return answer.toString();
    }
}
